package com.ruoyi.web.controller.system;


import com.ruoyi.web.service.C2ReleaseVersionOverviewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/C2ReleaseVersionOverview")
public class C2ReleaseVersionOverviewController {

    @Autowired
    private C2ReleaseVersionOverviewService c2ReleaseVersionOverviewService;

    @GetMapping("/byMonth")
    public ResponseEntity<Map<String, Integer>> getVersionFunnelData() {
        try {
            // 调用 Service 层获取数据
            Map<String, Integer> funnelData = c2ReleaseVersionOverviewService.getVersionFunnelData();

            // 判断是否有数据
            if (funnelData == null || funnelData.isEmpty()) {
                return ResponseEntity.status(HttpStatus.NO_CONTENT).body(Collections.emptyMap());
            }

            // 按顺序构造结果
            Map<String, Integer> orderedResult = new LinkedHashMap<>();
            orderedResult.put("totalVersions", funnelData.get("totalVersions"));
            orderedResult.put("releasedVersions", funnelData.get("releasedVersions"));
            orderedResult.put("publishVersions", funnelData.get("publishVersions"));

            // 返回按顺序的结果
            return ResponseEntity.ok(orderedResult);

        } catch (Exception e) {
            // 捕获异常并返回空结果，保持泛型一致
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(Collections.emptyMap());
        }
    }
}


